package com.sq.goodline.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sq.goodline.controller.dto.TransResponse;

@ControllerAdvice
public class GlobalExceptionHandler {
	
	private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
	
	/**
	 * 自定义异常捕获封装
	 * @param ex
	 * @return
	 */
	@ResponseBody
    @ExceptionHandler(value = TransException.class)
    public TransResponse<?> exceptionHandler(TransException ex) {
		logger.info("捕获到错误提示：{}-{}",ex.getErrorCode(),ex.getErrorMessage());
        return new TransResponse(ex.getErrorCode(),ex.getErrorMessage());
    }

	/**
	 * 系统异常捕获封装
	 * @param ex
	 * @return
	 */
	@ResponseBody
    @ExceptionHandler(value = Exception.class)
    public TransResponse<?> javaExceptionHandler(Exception ex) {//TransResponse是项目中对外统一的出口封装
        logger.error("捕获到Exception异常",ex);
        return new TransResponse("99","系统异常："+ex.getMessage());
    }

}
